package com.vm.osm.business.service.impl;

import com.vm.osm.business.dto.eam.equ.EamEqu;
import com.vm.osm.business.dto.eam.equ.GetEamEquReq;
import com.vm.osm.business.dto.eam.equ.SearchEamEquReq;
import com.vm.osm.business.dto.eam.equ.SearchEamEquRsp;
import com.vm.osm.business.dto.eam.equ_part.EamEquPart;
import com.vm.osm.business.dto.eam.equ_part.GetEamEquPartReq;
import com.vm.osm.business.dto.req.logic_equ.PageAssetEquReq;
import com.vm.osm.business.dto.req.logic_equ.GetAssetEquDetailReq;
import com.vm.osm.business.dto.req.logic_equ.SearchCustomerReq;
import com.vm.osm.business.dto.rsp.asset_equ.AssetEquDetail;
import com.vm.osm.business.dto.rsp.asset_equ.PageAssetEquRsp;
import com.vm.osm.business.dto.rsp.logic_equ.SearchCustomerRsp;
import com.vm.osm.business.entity.OsmLogicEqu;
import com.vm.osm.business.service.IAssetEquService;
import com.vm.osm.business.service.IOsmLogicEquService;
import com.vm.osm.business.service.IOsmServerProviderService;
import com.vm.osm.business.service.eam.IEamEquService;
import com.vm.osm.business.service.eam.IEamEquPartService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class AssetEquServiceImpl implements IAssetEquService {
    private static final Logger LOG = LoggerFactory.getLogger(AssetEquServiceImpl.class);
    @Autowired
    private IEamEquService eamEquInfoService;
    @Autowired
    private IOsmLogicEquService osmLogicEquService;
    @Autowired
    private IEamEquPartService eamEquPartInfoService;
    @Autowired
    private IOsmServerProviderService osmServerProviderService;

    @Override
    public PageAssetEquRsp page(PageAssetEquReq request) {
        if (null == request) {
            LOG.warn("【设备】（分页查询）请求参数为空");
            return null;
        }
        SearchEamEquReq searchreq = new SearchEamEquReq();
        BeanUtils.copyProperties(request, searchreq);
        searchreq.setEnterprise_id(request.getBelonger_id());
        searchreq.setSelect_type(request.getField_type());
        searchreq.setSelect_value(request.getField_value());
        SearchEamEquRsp searchrsp = eamEquInfoService.search(searchreq);
        if (null == searchrsp) {
            LOG.warn("【设备】（分页查询）请求资产平台应答为空");
            return null;
        }
        PageAssetEquRsp response = new PageAssetEquRsp();
        BeanUtils.copyProperties(searchrsp, response);
        response.setData_list(new LinkedList<>());
        Map<String, Object> params = new HashMap<>();
        searchrsp.getData_list().forEach(item -> {
            PageAssetEquRsp.AssetEquRow row = new PageAssetEquRsp.AssetEquRow();
            BeanUtils.copyProperties(item, row);
            params.clear();
            params.put("equ_code", item.getEqu_code());
            OsmLogicEqu equ = osmLogicEquService.get(params);
            row.setImported(null == equ ?
                    PageAssetEquRsp.AssetEquRow.Imported.NO : PageAssetEquRsp.AssetEquRow.Imported.YES);
            response.getData_list().add(row);
        });
        return response;
    }

    @Override
    public AssetEquDetail detail(GetAssetEquDetailReq request) {
        if (null == request) {
            LOG.warn("【企业】（获取设备详情）请求参数为空");
            return null;
        }
        GetEamEquReq getreq = new GetEamEquReq();
        BeanUtils.copyProperties(request, getreq);
        EamEqu eamEqu = eamEquInfoService.get(getreq);
        if (null == eamEqu) {
            LOG.warn("【企业】（获取设备详情）请求资产平台应答为空");
            return null;
        }
        AssetEquDetail detail = new AssetEquDetail();
        BeanUtils.copyProperties(eamEqu, detail);
        detail.setEqu_type(eamEqu.getIndustry_type());
        // 转译通用属性
        if (eamEqu.getCommon_attr_list() != null) {
            detail.setCommon_attrs(new LinkedList<>());
            eamEqu.getCommon_attr_list().forEach(item -> {
                AssetEquDetail.CommonAttr attr = new AssetEquDetail.CommonAttr();
                attr.setAttr_field(item.getField_key());
                attr.setAttr_val(item.getField_value());
                detail.getCommon_attrs().add(attr);
            });
        }
        // 转译自定义属性
        if (eamEqu.getDiy_field_list() != null) {
            detail.setDiy_attrs(new LinkedList<>());
            eamEqu.getDiy_field_list().forEach(item -> {
                AssetEquDetail.DiyAttr attr = new AssetEquDetail.DiyAttr();
                attr.setId(item.getId());
                attr.setAttr_field(item.getField_title());
                attr.setAttr_val(item.getField_value());
                detail.getDiy_attrs().add(attr);
            });
        }
        // 转译配置信息
        if (eamEqu.getEqu_part_list() != null) {
            detail.setParts(new LinkedList<>());
            eamEqu.getEqu_part_list().forEach(item -> {
                GetEamEquPartReq req = new GetEamEquPartReq();
                req.setSession_id(request.getSession_id());
                req.setId(item.getId());
                EamEquPart part_info = eamEquPartInfoService.get(req);
                if (null == part_info) {
                    LOG.warn("【企业】（获取设备配件详情）请求资产平台应答为空");
                    return;
                }
                AssetEquDetail.EquPart part = new AssetEquDetail.EquPart();
                BeanUtils.copyProperties(part_info, part);
                detail.getParts().add(part);
            });
        }
        return detail;
    }

    @Override
    public List<SearchCustomerRsp> searchCustomer(SearchCustomerReq request) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_id", request.getCompany_id());
        if (StringUtils.isNotEmpty(request.getCustomer_name())) {
            params.put("provider_name_blur", request.getCustomer_name());
        }
        params.put("status", "00001");
        List<SearchCustomerRsp> customers = new LinkedList<>();
        osmServerProviderService.find(params).forEach(item -> {
            SearchCustomerRsp record = new SearchCustomerRsp();
            record.setCustomer_id(item.getProviderId());
            record.setCustomer_name(item.getProviderName());
            customers.add(record);
        });
        return customers;
    }
}
